package com.esms.esmsadmin.service;

import com.esms.esmsadmin.entity.AdminEntity;
import com.esms.esmsadmin.entity.MenuEntity;
import com.esms.esmsadmin.mapper.MenuMapper;
import com.esms.esmsadmin.mapper.MenuRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MenuService {

    @Autowired
    MenuMapper menuMapper;

    @Autowired
    MenuRoleMapper menuRoleMapper;
    public List<MenuEntity> getMenusByAdminId() {
        return menuMapper.getMenusByAdminId(((AdminEntity) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal()).getId());
    }

    public List<MenuEntity> getAllMenus() {
        return menuMapper.getAllMenus();
    }

    public List<Integer> getMidsByRid(Integer rid) {
        return menuMapper.getMidsByRid(rid);
    }

    @Transactional
    public boolean updateMenuRole(Integer rid, Integer[] mids) {
        menuRoleMapper.deleteByRid(rid);
        if(mids == null || mids.length == 0)
            return true;
        Integer result = menuRoleMapper.insertRecord(rid,mids);
        return result == mids.length;
    }
}


